(function($) {
	var CmdUtils = {
		getIP : function() {
			var ip = '';
			$.ajax({
				type : 'POST',
				url : $E.url + '/getIP_',
				data : {
					staffId : $E.staff.id
				},
				async : false
			}).done(function(data) {
				ip = data;
			})
			return ip;
		},

		guid : function() {
			return Guid.raw();
		},

		randomInt : function(min, max) {
			return parseInt(Math.random() * (max - min) + min);
		},

		randomFloat : function(min, max) {
			return Math.random() * (max - min) + min;
		},

		randomStr : function(length) {
			var len = len || 32;
			var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
			var str = '';
			for (var i = 0; i < len; i++) {
				str += $chars.charAt(Math.floor(Math.random() * $chars.length));
			}
			return str;
		},

		ID6Gen : function() {
			var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
			var id = '';
			for (var i = 0; i < 6; i++) {
				id += $chars.charAt(Math.floor(Math.random() * $chars.length));
			}
			return id;
		},

		/**
		 * 写入一个全局私有数据
		 * @method setPublicValue
		 * @param runtime {object} 当前页面的运行环境
		 * @param key {string} 私有数据的key 
		 * @param value {any} 私有数据的值
		 */
		setPublicValue : function(runtime, key, value) {
			let publicCache = JSON.parse(window.sessionStorage.getItem($CONST.CacheName.PUBLIC_CACHE));
			publicCache[key.toLowerCase()] = value;
 			window.sessionStorage.setItem($CONST.CacheName.PUBLIC_CACHE, JSON.stringify(publicCache));

			var rootData = new BoundData("Root"),
				dataBound = new BoundData("TAB_COOKIES" + $E.staff.epidSql);
			dataBound.setKey("STAFF_ID", $E.staff.id);
			dataBound.setKey("SCOPE", "PUBLIC");
			dataBound.setKey("COOKIEKEY", key.toLowerCase());
			dataBound.setValue("COOKIEVALUE", value);
			rootData.addSub("TAB_COOKIES", dataBound);
			$F.dataService('SystemDo5', {
				data : {
					token : runtime.project.token,
					dbPool : '',
					xml : rootData.getData()
				},
				digest: runtime.project.digest,
				success : function(data) {
					if (data.code < 0)
						$F.err("CmdUtils - setPublicValue", data.message);
				}
			});
		},

		/**
		 * 读取一个全局私有数据，全局私有数据在NGS项目中有效
		 * @method getPublicValue
		 * @param key {string} 私有数据的key
		 * @param defaultValue {any} 当该私有数据不存在时，返回defaultValue
		 * @return {any} 私有数据的值
		 */
		getPublicValue : function(key, defaultValue) {
			let publicCache = JSON.parse(window.sessionStorage.getItem($CONST.CacheName.PUBLIC_CACHE));
			let value = publicCache[key.toLowerCase()];
			if (value == undefined || value == null || value === '')
				return (defaultValue == undefined ? '' : defaultValue);
			return value;
		},

		/**
		 * 写入一个当前页面的私有数据
		 * @method setLocalValue
		 * @param runtime {object} 当前页面的运行环境
		 * @param key {string} 私有数据的key 
		 * @param value {any} 私有数据的值
		 */
		setLocalValue : function(runtime, key, value) {
			if (!runtime.instance.storage[runtime.struct.guid])
				runtime.instance.storage[runtime.struct.guid] = {};
			runtime.instance.storage[runtime.struct.guid][key.toLowerCase()] = value;

			var rootData = new BoundData("Root"),
				dataBound = new BoundData("TAB_COOKIES" + $E.staff.epidSql);
			dataBound.setKey("STAFF_ID", $E.staff.id);
			dataBound.setKey('SCOPE', runtime.namespace);
			dataBound.setKey("COOKIEKEY", key.toLowerCase());
			dataBound.setValue("COOKIEVALUE", value);
			rootData.addSub("TAB_COOKIES", dataBound);
			$F.dataService('SystemDo5', {
				data : {
					token : runtime.project.token,
					dbPool : '',
					xml : rootData.getData()
				},
				digest: runtime.project.digest,
				success : function(data) {
					if (data.code < 0)
						$F.err("CmdUtils - setLocalValue", data.message);
				}
			});
		},

		/**
		 * 读取一个当前页面中的私有数据
		 * @method getValue
		 * @param key {string} 私有数据的key  
		 * @param defaultValue {any} 当该私有数据不存在时，返回defaultValue
		 * @param runtime {object} 当前页面的运行环境
		 * @return {any} 私有数据的值
		 */
		getLocalValue : function(key, defaultValue, runtime) {
			key = key.toLowerCase();
			var value;
			if (runtime && runtime.instance.storage[runtime.struct.guid])
				value = runtime.instance.storage[runtime.struct.guid][key];
			if (value == undefined || value === '')
				return (defaultValue == undefined ? '' : defaultValue);
			return value;
		},

		/**
		 * 读取一个私有数据。当前页面中如果不存在该私有数据，则自动在全局中加载该私有数据
		 * @method getValue
		 * @param key {string} 私有数据的key  
		 * @param defaultValue {any} 当该私有数据不存在时，返回defaultValue
		 * @param runtime {object} 当前页面的运行环境
		 * @return {any} 私有数据的值
		 */
		getValue : function(key, defaultValue, runtime) {
			key = key.toLowerCase();
			var value;
			if (runtime && runtime.instance.storage[runtime.struct.guid])
				value = runtime.instance.storage[runtime.struct.guid][key];
			if (value == undefined || value === '')
				return this.getPublicValue(key, defaultValue);
			else
				return value;
		},

		propertyListener : function(runtime, propertyName, cmdId) {
			runtime.instance.propertyListener('CMDListener-' + runtime.struct.id + '.' + cmdId, function(e, data) {
				if (e == propertyName)
					runtime.instance.doCmd('P' + cmdId, data, runtime)
			})
		},

		isNullDate : function(date) {
			if ($.type(date) == 'string') {
				return date.startWith('1900') || date.trim() == '';
			} else {
				if (date instanceof Date) {
					return date.getFullYear() == 1900
				}
				return true;
			}
		},

		getDataObject : function(project, customer, objective) {
			// 整合全局变量数据
			var ev = {
				EPID : $E.staff.epid,
				EPIDTB : $E.staff.epidSql,
				STAFF : $E.staff.id,
				STAFFNAME : $E.staff.name,
				AGENTID : $E.staff.agentId,
				ROLEGROUPID : project.groupId,
				ROLEGROUPNAME : project.groupName
			};
			if (project.cwInfo && project.cwInfo.callInfo && project.cwInfo.callInfo.getValue) {
				ev.ANI = project.cwInfo.callInfo.getValue("ani");
				ev.DNIS = project.cwInfo.callInfo.getValue("dnis");
				ev.EXTENSION = project.cwInfo.callInfo.getValue("extension");
			} else {
				ev.ANI = '';
				ev.DNIS = '';
				ev.EXTENSION = '';
			}
			if (project.cwInfo) {
				ev.CALLTYPE = project.cwInfo.callType;
				ev.CUREVENTID = project.cwInfo.eventGuid;
			}

			// 整合客户对象数据
			var cust = {};
			if (customer) {
				for (var key in customer.values) {
					cust[key.toLowerCase()] = customer.values[key]
				}
			}

			// console.log(runtime.instance.objective.fields)
			var obj = {};
			if (objective) {
				for (var field in objective.fields) {
					obj[field.toLowerCase()] = objective.getValue(field);
				}
			}

			return {
				pub : ev,
				cust : cust,
				obj : obj
			}
		},

		getInstanceData : function() {
			var project = $E.getActiveProject();
			var customer = project.customer;
			var objective = {};
			return this.getDataObject(project, customer, objective);
		},

		getDayBegin : function(date) {
			date = $F.parseDate(date);
			var t = new Date(date.getTime());
			t.setHours(0);
			t.setMinutes(0);
			t.setSeconds(0);
			t.setMilliseconds(0);
			return t;
		},

		getDayEnd : function(date) {
			date = $F.parseDate(date);
			var t = new Date(date.getTime());
			t.setHours(23);
			t.setMinutes(59);
			t.setSeconds(59);
			t.setMilliseconds(999);
			return t;
		}
	}
	window.CmdUtils = CmdUtils;
})(jQuery)